package com.concerneyes.mine.TabActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.mine.Task.AdressImpl;
import com.concerneyes.mine.Task.AdressListener;
import com.concerneyes.mine.adapter.MyAdressAdapter;
import com.concerneyes.mine.model.MyAdressModel;

import java.util.ArrayList;
import java.util.List;

import common.AbsActivity;
import view.MyUniversalDialog;

/**
 * Created by Administrator on 2017/7/1 0001.
 */

/**
 * 我的地址列表
 */
public class MyAdressActivity extends AbsActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener,AdressListener {
    private ListView lv_my_adress;
    private MyAdressAdapter adapter;
    private List<MyAdressModel> myModel;
    private AdressImpl impl ;
    private boolean isAdress = true;
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_adress);
        String title = getIntent().getStringExtra("title");

        if (!TextUtils.isEmpty(title)) {
            setTitle(title);
            if (title.equals(getResources().getString(R.string.my_adress_))){
                isAdress = true;
            }else {
                isAdress = false;
            }
        }
        initView();
    }
    private void initView() {
        lv_my_adress = (ListView) findViewById(R.id.lv_my_adress);
        View footerView = LayoutInflater.from(this).inflate(R.layout.item_list_me, null);
        View view_diver = footerView.findViewById(R.id.view_diver);
        RelativeLayout rl_item = (RelativeLayout) footerView.findViewById(R.id.rl_item);
        TextView tv_me_title = (TextView) footerView.findViewById(R.id.tv_me_title);
        ImageView iv_me_icons = (ImageView) footerView.findViewById(R.id.iv_me_icons);
        tv_me_title.setText(R.string.add_adress);
        rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adressIntent("add",0);
            }
        });
        view_diver.setVisibility(View.VISIBLE);
        lv_my_adress.addFooterView(footerView);

        impl =new AdressImpl(this,this);
        impl.listAdress();
        lv_my_adress.setOnItemClickListener(this);
        lv_my_adress.setOnItemLongClickListener(this);
        myModel = new ArrayList<MyAdressModel>();
    }
    @Override
    protected void onResume() {
        super.onResume();
        impl.listAdress();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (isAdress){
            adressIntent("change",position);
        }else {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("MyAdressModel",adapter.getItem(position));
            intent.putExtras(bundle);
            setResult(20, intent);
            finish();
        }

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        ChangeAdressDialog(position);
        return true;
    }

    private void ChangeAdressDialog(final int position) {
        final MyUniversalDialog dialog = new MyUniversalDialog(this);
        int width = getWindowManager().getDefaultDisplay().getWidth();
        dialog.setLayout(LayoutInflater.from(this).inflate(R.layout.dialog_adress, null), MyUniversalDialog.DialogGravity.CENTER, -2, 5 * width / 6);
        dialog.setCanceledOnTouchOutside(true);
        TextView change = (TextView) dialog.findViewById(R.id.change);
        change.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                adressIntent("change", position);
                dialog.dismiss();
            }
        });

        TextView detele = (TextView) dialog.findViewById(R.id.detele);
        detele.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int id = myModel.get(position).getId();
                impl.deteleAdress(id);
                dialog.dismiss();
            }
        });
        TextView set_mr = (TextView) dialog.findViewById(R.id.set_mr);
        set_mr.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int id = myModel.get(position).getId();
                impl.setDefaultAdress(id);
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    private void adressIntent(String adress_style, int position) {
        Intent intent = new Intent(this, AdressManageActivity.class);
        intent.putExtra("adress_style",adress_style);
        if (adress_style .equals("change")) {
            intent.putExtra("adress_position", position);
        }
        startActivityForResult(intent, 100);
    }

    @Override
    public void adressListSuccess(List<MyAdressModel> model) {
        myModel = model;
        adapter = new MyAdressAdapter(MyAdressActivity.this);
        adapter.clear();
        adapter.addItems(model);
        lv_my_adress.setAdapter(adapter);
    }

    @Override
    public void adressListFail() {

    }

    @Override
    public void deleteAdressSuccess(List<MyAdressModel> model) {
        adapter.clear();
        adapter.addItems(model);
        lv_my_adress.setAdapter(adapter);
    }

    @Override
    public void deleteAdressFail() {

    }

    @Override
    public void setDefaultAdressSuccess(List<MyAdressModel> model) {
        adapter.clear();
        adapter.addItems(model);
        lv_my_adress.setAdapter(adapter);
    }

    @Override
    public void setDefaultAdressFail() {

    }

    @Override
    public void addAdressSuccess() {

    }

    @Override
    public void addAdressFail() {

    }
}
